package com.buba.user.service.impl;

import com.buba.pojo.user.Inpatient;
import com.buba.pojo.user.ThePatient;
import com.buba.user.mapper.InpatientMapper;
import com.buba.user.mapper.ThePatientMapper;
import com.buba.user.service.InpatientService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.buba.user.vo.InpatientVo;
import com.buba.utils.RandomUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.List;

/**
 * <p>
 * 住院人表 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2022-06-09
 */
@Service
@Slf4j
public class InpatientServiceImpl extends ServiceImpl<InpatientMapper, Inpatient> implements InpatientService {

    @Autowired
    private InpatientMapper inpatientMapper;

    @Autowired
    private ThePatientMapper thePatientMapper;

    @Override
    public List<InpatientVo> inpatientUserId(Integer id) {
        List<InpatientVo> inpatients = inpatientMapper.inpatientUserId(id);
        return inpatients;
    }

    @Override
    public InpatientVo hospitalById(Integer id) {
        InpatientVo hospital = inpatientMapper.hospitalById(id);
        log.info("hospital.getCampus()::"+hospital);
        if (hospital == null){
            ThePatient thePatient = thePatientMapper.selectById(id);
            InpatientVo inpatientVo = new InpatientVo();
            InpatientVo patient = inpatientVo.setThePatient(thePatient);
            return patient;
        }
        return hospital;
    }

    @Override
    public boolean bindInpatient(Integer id) {
        Integer userId = thePatientMapper.selectById(id).getUserId();
        Inpatient inpatient = new Inpatient();
        inpatient.setPatientId(id);
        inpatient.setUserId(userId);
        inpatient.setDepartmentId(3);
        inpatient.setStatus("住院中");
        inpatient.setAdmissionNumber(RandomUtil.getSixBitRandom());
        Calendar calendar = Calendar.getInstance();//获取时间
        inpatient.setHospitalizedTime(calendar.getTime());
        inpatient.setCreationTime(calendar.getTime());
        inpatient.setModificationTime(calendar.getTime());

        int insert = baseMapper.insert(inpatient);

        if (insert != 0){
            return true;
        }else {
            return false;
        }

    }
}
